package com.txd.mofa.login;

import android.content.Context;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.kongzue.baseframework.interfaces.Layout;
import com.kongzue.baseframework.util.AppManager;
import com.kongzue.baseframework.util.JumpParameter;
import com.txd.mofa.R;
import com.txd.mofa.adapter.FragmentAdp;
import com.txd.mofa.base.BaseAty;
import com.txd.mofa.net.NetUtils;
import com.txd.mofa.view.HXLinePagerIndicator;
import com.txd.mofa.view.MyViewPager;
import com.txd.mofa.view.ScaleTransitionPagerTitleView;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.UIUtil;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import java.util.ArrayList;
import java.util.List;

@Layout(R.layout.aty_xieyi_zhengce)
public class XieyiZhengceAty extends BaseAty {

    MyViewPager viewpager;
    private MagicIndicator magicIndicator;
    private ImageView iv_back,iv,iv1;
    private LinearLayout ll_tongyi,ll_tongyi1,ll;
    private TextView tv_tongyi,tv_jujue,tv_tongyi1,tv_jujue1;

    private List<String> listTitle = new ArrayList<>();
    private List<Fragment> list = new ArrayList<>();
    private String first;
    private int pos;
    private String flag2 = "";

    @Override
    public void initViews() {
        super.initViews();
        tv_tongyi = findViewById(R.id.tv_tongyi);
        tv_jujue = findViewById(R.id.tv_jujue);
        tv_tongyi1 = findViewById(R.id.tv_tongyi1);
        tv_jujue1 = findViewById(R.id.tv_jujue1);

        ll = findViewById(R.id.ll);
        iv = findViewById(R.id.iv);
        iv1 = findViewById(R.id.iv1);
        ll_tongyi1 = findViewById(R.id.ll_tongyi1);
        iv_back = findViewById(R.id.iv_back);
        ll_tongyi = findViewById(R.id.ll_tongyi);
        magicIndicator = findViewById(R.id.magic_indicator);
        viewpager = findViewById(R.id.viewpager);


        listTitle.add("用户协议");
        listTitle.add("隐私政策");


        for (int i = 0; i < listTitle.size(); i++) {
            list.add(XieyiFgt.newInstance(i+""));
        }

        FragmentAdp fragmentAdp = new FragmentAdp(getSupportFragmentManager(), list, listTitle);
        viewpager.setAdapter(fragmentAdp);
        viewpager.setOffscreenPageLimit(list.size());

        CommonNavigator commonNavigator = new CommonNavigator(me);

        //true 选项卡均分父布局宽度。这种情况仅仅在选项卡较少且文字比较短时候合适
        //     如果文字很长，且选项卡很多，那么久会挤在一起密密麻麻，视觉上比较难看。
        //通常设置false。
        commonNavigator.setAdjustMode(false);

        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return listTitle == null ? 0 : listTitle.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {

//                SimplePagerTitleView simplePagerTitleView = new SimplePagerTitleView(context);
                SimplePagerTitleView simplePagerTitleView = new ScaleTransitionPagerTitleView(context); //使用自定义
                simplePagerTitleView.setText(listTitle.get(index));
                simplePagerTitleView.setTextSize(19);
                simplePagerTitleView.setNormalColor(getResources().getColor(R.color.hui));
                simplePagerTitleView.setSelectedColor(getResources().getColor(R.color.huang));
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        viewpager.setCurrentItem(index);
                    }
                });

                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                HXLinePagerIndicator indicator = new HXLinePagerIndicator(context);
                indicator.setMode(LinePagerIndicator.MODE_EXACTLY);
                indicator.setLineHeight(UIUtil.dip2px(context, 4));
                indicator.setLineWidth(UIUtil.dip2px(context, 20));
                indicator.setRoundRadius(UIUtil.dip2px(context, 3));

                indicator.setStartInterpolator(new AccelerateInterpolator());
                indicator.setEndInterpolator(new DecelerateInterpolator(2.0f));

                return indicator;
            }
        });

        magicIndicator.setNavigator(commonNavigator);

        ViewPagerHelper.bind(magicIndicator, viewpager);


        viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                Log.e("====",position+"");
                if (first.equals("111")) {
                    if (position == 0) {
                        ll_tongyi.setVisibility(View.VISIBLE);
                        ll_tongyi1.setVisibility(View.GONE);
                    } else {
                        ll_tongyi.setVisibility(View.GONE);
                        ll_tongyi1.setVisibility(View.VISIBLE);
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    protected void onNetworkConnected(NetUtils.NetType type) {

    }

    @Override
    protected void onNetworkDisConnected() {

    }

    @Override
    public void initDatas(JumpParameter paramer) {

        pos = paramer.getInt("pos");

        first = paramer.getString("first");

        if (first == null || first.equals("")){
            pos = getIntent().getIntExtra("pos",0);
            first = getIntent().getStringExtra("first");
            flag2 = getIntent().getStringExtra("flag");
        }

        if (first.equals("111")){
            iv_back.setVisibility(View.GONE);

            if (pos == 0){
                ll_tongyi.setVisibility(View.VISIBLE);
                ll_tongyi1.setVisibility(View.GONE);
            }else {
                ll_tongyi.setVisibility(View.GONE);
                ll_tongyi1.setVisibility(View.VISIBLE);
            }

        }else {
            iv_back.setVisibility(View.VISIBLE);
            ll_tongyi.setVisibility(View.GONE);
            ll_tongyi1.setVisibility(View.GONE);
        }



        viewpager.setCurrentItem(pos);

    }

    private boolean flag = false;
    private boolean flag1 = false;


    @Override
    public void setEvents() {

        tv_jujue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AppManager.getInstance().killAllActivity();
                finish();
            }
        });
        tv_jujue1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AppManager.getInstance().killAllActivity();
                finish();
            }
        });
        tv_tongyi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (flag == false){
                    iv.setImageResource(R.mipmap.tongyi);
                    flag = true;
                    if (flag1 == true){
                        if (flag2.equals("111")){
                            finish();
                        }else {
                            if (first.equals("111")) {
//                                jump(LoginAty.class);
                                startActivity(new Intent(XieyiZhengceAty.this, BootPageAty.class));
                                finish();
                            } else {
                                finish();
                            }
                        }
                    }else {
                        viewpager.setCurrentItem(1);
                    }
                }else {
                    iv.setImageResource(R.mipmap.weitongyi);
                    flag = false;
                }
            }
        });

        tv_tongyi1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (flag1 == false){
                    iv1.setImageResource(R.mipmap.tongyi);
                    flag1 = true;
                    if (flag == true){
                        if (flag2.equals("111")){
                            finish();
                        }else {
                            if (first.equals("111")) {
//                                jump(LoginAty.class);
                                startActivity(new Intent(XieyiZhengceAty.this, BootPageAty.class));

                                finish();
                            } else {
                                finish();
                            }
                        }
                    }else {
                        viewpager.setCurrentItem(0);
                    }
                }else {
                    iv1.setImageResource(R.mipmap.weitongyi);
                    flag1 = false;
                }
            }
        });


        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}
